package com.weetop.cfw.base.presenter.imp

import com.blankj.utilcode.util.LogUtils
import com.weetop.cfw.base.presenter.CourseDetailPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.CourseDetailView
import com.weetop.cfw.bean.*
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.mine.activity.CourseDetailActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class CourseDetailPresenterImp(var courseDetailView: CourseDetailView? = null):CourseDetailPresenter {

    /**
     * 获取视频详情
     */
    override fun getCourseDetail(courseDetailActivity: CourseDetailActivity,infoid:Int) {
        courseDetailActivity.showLoadingDialog(courseDetailActivity,"正在获取商业云课详情...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getCloudCurriculumDetail(infoid),courseDetailActivity,object:RxJavaCallBack<AudioVideoDetailBean>(){
            override fun onSuccess(t: AudioVideoDetailBean?) {
                courseDetailActivity.dismissLoadingDialog()
                courseDetailView?.courseDetailDataGetSuccess(t)
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                courseDetailActivity.dismissLoadingDialog()
            }

            override fun onRequestError(t: AudioVideoDetailBean?) {
                super.onRequestError(t)
                courseDetailActivity.dismissLoadingDialog()
            }

        })
    }

    /**
     * 上传商业云课浏览记录
     */
    override fun uploadUserAudioAndVideoBrowsingRecords(courseDetailActivity: CourseDetailActivity, infoid: String) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().uploadUserAudioAndVideoBrowsingRecordsData(infoid = infoid),courseDetailActivity,object:RxJavaCallBack<ErrorBean>(){
            override fun onSuccess(t: ErrorBean?) {
                if(t != null){
                    LogUtils.dTag("上传商业云课浏览记录",t.message)
                }
            }

            override fun onRequestError(t: ErrorBean?) {
                super.onRequestError(t)
                if(t != null){
                    LogUtils.dTag("上传商业云课浏览记录",t.message)
                }
            }

        })
    }

    /**
     * 获取订单编号
     */
    override fun getOrderId(courseDetailActivity: CourseDetailActivity, infoid: Int) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getOrderId(Infoid = infoid),courseDetailActivity,object:RxJavaCallBack<OrderIdBean>(){

            override fun onSuccess(t: OrderIdBean?) {
                if(t != null){
                    courseDetailView?.getOrderIdSuccess(t.orderid)
                }
            }

            override fun onRequestError(t: OrderIdBean?) {
                super.onRequestError(t)
                if(t != null){
                    courseDetailView?.showNativeShortToast("订单编号获取失败")
                }
            }

        })
    }

    /**
     * 获取订单支付信息
     */
    override fun getOrderInfo(courseDetailActivity: CourseDetailActivity, orderId: String) {
        courseDetailActivity.showLoadingDialog(courseDetailActivity,"正在下单...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getOrderInfo(orderId = orderId),courseDetailActivity,object:RxJavaCallBack<OrderInfoBean>(){

            override fun onSuccess(t: OrderInfoBean?) {
                if(t != null){
                    courseDetailActivity.dismissLoadingDialog()
                    courseDetailView?.orderInfoGetSuccess(t)
                }
            }

            override fun onRequestError(t: OrderInfoBean?) {
                super.onRequestError(t)
                if(t != null){
                    courseDetailView?.showNativeShortToast("下单失败")
                    courseDetailActivity.dismissLoadingDialog()
                }
            }

        })
    }

    /**
     * 获取推荐视频数据
     */
    override fun getRecommendVideoData(courseDetailActivity: CourseDetailActivity, classId: Int) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getRecommendVideoData(classid = classId),courseDetailActivity,object:RxJavaCallBack<RecommendAudioVideoBean>(){
            override fun onSuccess(t: RecommendAudioVideoBean?) {
                courseDetailView?.getRecommendVideoSuccess(t)
            }
        })
    }

    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        courseDetailView = baseView as? CourseDetailView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        courseDetailView = null
    }
}